package com.neusoft.yuejian12.yuejian_12.fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.AddFeedbackActivity;

import com.neusoft.yuejian12.yuejian_12.activity.ModifyUserActivity;

import com.neusoft.yuejian12.yuejian_12.activity.MyCollectListActivity;
import com.neusoft.yuejian12.yuejian_12.activity.MyTrainOrderListActivity;
import com.neusoft.yuejian12.yuejian_12.activity.MyPostActivity;
import com.neusoft.yuejian12.yuejian_12.constant.Constant;
import com.neusoft.yuejian12.yuejian_12.javabean.UserBean;
import com.neusoft.yuejian12.yuejian_12.model.UserModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.USER_PHOTO_URL;

/**
 * Created by Administrator on 2017/9/26.
 */

public class MyFragment extends Fragment {
    private TextView u_name,logout;
    private ImageView u_pic;
    private TextView modify_userinfo;
    private TextView addfeedback;
    private View view = null;
    private int u_id;
    private Button mypost_button, mycollect_button, mydeal_button;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab05, container, false);
        initViews();
        initData();
        initEvents();
        //modifyData();
        return view;
    }

    public void initViews() {
        u_name = (TextView) view.findViewById(R.id.u_name);
        u_pic = (ImageView) view.findViewById(R.id.u_pic);
        modify_userinfo = (TextView) view.findViewById(R.id.modify_userinfo);
        addfeedback = (TextView) view.findViewById(R.id.addfeedback);
        mypost_button = (Button) view.findViewById(R.id.mypost_button);
        mycollect_button = (Button) view.findViewById(R.id.mycollect_button);
        mydeal_button = (Button) view.findViewById(R.id.mydeal_button);
        logout= (TextView) view.findViewById(R.id.logout);

    }

    public void initEvents() {
        modify_userinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MyFragment.this.getActivity(), ModifyUserActivity.class);
                startActivity(intent);
            }
        });

        addfeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MyFragment.this.getActivity(), AddFeedbackActivity.class);
                startActivity(intent);
            }
        });


        mypost_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View view){
            Intent intent = new Intent();
            intent.setClass(MyFragment.this.getActivity(), MyPostActivity.class);
            startActivity(intent);
        }
       });

        mydeal_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                Intent intent = new Intent();
                intent.setClass(MyFragment.this.getActivity(), MyTrainOrderListActivity.class);
                startActivity(intent);
            }
        });


        mycollect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                Intent intent = new Intent();
                intent.setClass(MyFragment.this.getActivity(), MyCollectListActivity.class);
                startActivity(intent);
            }
        });

        logout.setOnClickListener(new View.OnClickListener() {
           @Override
          public void onClick (View view){
               new AlertDialog.Builder(getActivity()).setTitle("系统提示")//设置对话框标题
                       .setMessage("确定要退出系统？")//设置显示的内容
                       .setPositiveButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮
                           @Override
                           public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                             System.exit(0);
                           }
                       }).setNegativeButton("返回",new DialogInterface.OnClickListener() {//添加返回按钮
                   @Override

                   public void onClick(DialogInterface dialog, int which) {//响应事件
                       dialog.dismiss();
                   }

               }).show();//在按键响应事件中显示此对话框
           }


        });


    }

    public void initData(){
        SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id",0);
      // Log.d("userbg","u_id");
        UserModel userModel = new UserModel();
        Call call = userModel.getUserInfo(u_id);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean user = response.body();
                u_name.setText(user.getU_name());
//               Log.d("userbg","username");
                String upic=USER_PHOTO_URL+user.getU_pic();
                Picasso.with(MyFragment.this.getActivity()).load(upic).into(u_pic);
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
            }
        });
    }

}

